//
//  CCRootController.m
//  ccZone
//
//  Created by 陈小娟 on 2017/12/13.
//  Copyright © 2017年 CC. All rights reserved.
//

#import "CCRootController.h"
#import "CCHomeController.h"
#import "CCMineController.h"
#import <ReactiveObjC.h>
#import "CCJumpManager.h"
#import "CCNavigationController.h"
#import "CCTabBar.h"
#import "CCPublishView.h"
@interface CCRootController ()<CCPublishViewDelegate>
{
    CCHomeController * _homeController;
    CCMineController * _mineController;
}

@property (nonatomic, assign) NSInteger remianLastSelect;

@property (nonatomic, assign) NSInteger currentSelect;

@end

@implementation CCRootController

static CCRootController *shareTabToot = nil;

+(instancetype)shareTabTootController {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareTabToot = [[self alloc]init];
        shareTabToot.selectedIndex = shareTabToot.currentSelect =0;
    });
    return shareTabToot;
}

-(void)addchild {
    _homeController = [[CCHomeController alloc]init];
    CCNavigationController *navhome = [[CCNavigationController alloc]initWithRootViewController:_homeController];
    navhome.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:kTYIMAGE(@"tab_home_unselect_icon") selectedImage:kTYIMAGE(@"tab_home_select_icon")];
    
    _mineController = [[CCMineController alloc]init];
    CCNavigationController *navmine = [[CCNavigationController alloc]initWithRootViewController:_mineController];
    navmine.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:kTYIMAGE(@"tab_mine_unselect_icon") selectedImage:kTYIMAGE(@"tab_mine_select_icon")];
    
    self.viewControllers = @[navhome,navmine];
    [self initJump];
    [self addtab];
}

-(void)initJump {
    WS(wSelf)
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:JumpToMineNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        //跳转
        [wSelf jumpToTabIndex:1];
    }];
    
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:JumpToHomeNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        [wSelf jumpToTabIndex:0];
    }];
}

- (void)jumpToTabIndex:(NSInteger)index
{
    CCNavigationController * nav = self.viewControllers[self.selectedIndex];
    [nav dismissViewControllersInStack];
    self.currentSelect = index;
    
    if(self.selectedIndex != index){
        self.selectedIndex = index;
    }
}

-(void)addtab {
    CCTabBar *myTabBar = [[CCTabBar alloc] init];
    @weakify(self);

    myTabBar.ComposeBtnBlock = ^(){
        @strongify(self)
        CCPublishView *pub = [[CCPublishView alloc]initWithFrame:CGRectMake(0, 0, PORTRAIT_MAIN_SCREEN_W, PORTRAIT_MAIN_SCREEN_H -34)];
        pub.delegate = self;
        [pub showWithController:self];
    };
    [self setValue:myTabBar forKey:@"tabBar"];
    [self setTabBarAttribute];
}

-(void)ccPublishView:(CCPublishView *)pub select:(NSUInteger)view {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addchild];
}

-(void)setTabBarAttribute {
//    UIView *tabbarView = [[UIView alloc] init];
//    CGFloat tabbarViewH = 0.f;
//    if (kScreenHeight == 812) {
//        tabbarViewH = 83;
//    }else{
//        tabbarViewH = 49;
//    }
//    tabbarView.frame = CGRectMake(0, 0, SCREEN_WIDTH, tabbarViewH);
//    tabbarView.backgroundColor = [UIColor whiteColor];
//    [self.tabBar insertSubview:tabbarView atIndex:0];
//    self.tabBar.translucent = NO;
//    self.tabBar.tintColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
